#!/usr/bin/env node
var db_user = require('../db/db_user');
var connect = require('../db/connect');
var connection = connect.connection;


module.exports.search = function(req, res){
    console.log('search!');
    var kw = req.query.kw;
    console.log(kw);
    //TODO: get search products and goes to that page.
    connection.query('select count(Name) as num from Product where Name like "%'+kw+'%"', 
        function(err,results){          
            console.log('results[0].num: ' +results[0].num);
            var num = results[0].num;   
            var lineNum = Math.floor(num%4==0 ? num/4 : num/4+1); //How may lines of products to show.
            console.log('lineNum: '+lineNum);
            connection.query('select * from Product where Name like "%'+kw+'%"',
                function(err, results){
                    console.log('render search')
                    res.redirect('/signin')
                    //res.render('list',{lineNum : lineNum,  num:num});
                   
                }
                             );
        }            
                     );
}
    //Temp: Don't do paging.
    //var totalNum; //Return the total num of requried products.
    //var numOfPage = totalNum%16 ? (totalNum/16+1) : totalNum/16;
    
    //res.render('search')
    
   //console.log(products);
    
   //res.redirect('/');  

module.exports.show = function(req, res){
    var pid = req.params.productid;
    console.log('pid: '+pid);
    var query = 'select * from Product where idProduct = '+pid;
    connection.query(query,
        function(err, results){
            console.log(results);
            res.render('product', results[0])
            });
    
     //res.render('product', {pName:p.name, price:p.price, pid:p.id})
}

module.exports.buy = function(req,res){
   //var pid = req.body.
}

